# -*- coding: UTF-8 -*-
import os
import time

import re
import threading
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException, ElementNotVisibleException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
from datetime import datetime, timedelta

from subprocess import call

from dash_lib.mongodb_helper import bulk_insert, bulk_insert_feed, bulk_insert_tipper, bulk_insert_email_tipper


def not_empty(s):
    return s and s.strip()


def fillter_tipper_id(email_view_item):
    print('email_view_item', email_view_item.text())
    print('email_view_item      aaaaaaaaaaaaaa       ', email_view_item.text())

    email_text = email_view_item.text()
    return email_text.replace('.blogabet.com', '')


def handle_tipper_list(is_email):
    html = driver.page_source
    resultList = []
    if html:
        doc = pq(html)
        split_array = doc('.tipster-box.row.no-padding-lg' if is_email else '.block.row.no-padding-lg').text().split('\n')
        filter_array = list(filter(lambda x: split_array.index(x) % 14, split_array))
        tipper_size = len(list(filter(lambda x: split_array.index(x) % 14 == 0, split_array)))
        for i in range(tipper_size):
            rel_index = 13 * i
            row_data = filter_array[rel_index:rel_index + 13]
            if not row_data:
                continue
            print(rel_index, len(row_data), row_data)
            replace = row_data[0].replace('.blogabet.com ', '').replace('.blogabet.com', '')
            try:
                inser_item = {'name': replace, 'since': row_data[1], 'picks': int(row_data[3]), 'profit': row_data[5],
                              'yield': row_data[7],
                              'verified': row_data[9], 'followers': row_data[11]}
                resultList.append(inser_item)
            except ValueError:
                pass
    else:
        print(u'抓取网页失败，跳过')
    return resultList


def loadNext(more_tag_txt):
    WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'{}')]".format(more_tag_txt))))

    driver.find_element_by_xpath("//a[contains(text(),'Show More')]").click()


def is_not_finished(more_tag_txt):
    try:
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'{}')]".format(more_tag_txt))))
        return True
    except TimeoutException:
        return False


def spread_all(more_tag_txt):
    while is_not_finished(more_tag_txt):
        loadNext(more_tag_txt)


def start_get_all_list():
    driver.get('https://blogabet.com/mytipsters')

    spread_all('Show More')
    tipper_list = handle_tipper_list(False)
    bulk_insert_tipper(tipper_list, True)


def start_get_email_list():
    driver.get('https://blogabet.com/emailalerts')
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//a[@aria-controls="Subscriptions"]')))

    driver.find_element_by_xpath('//a[@aria-controls="Subscriptions"]').click()
    spread_all('Show More')
    tipper_list = handle_tipper_list(True)
    bulk_insert_email_tipper(tipper_list)

def login():
    driver.get('https://blogabet.com')
    try:
        driver.find_element_by_xpath('//button[@id="impliedsubmit"]').click()
    except (NoSuchElementException, ElementNotVisibleException):
        pass

    time.sleep(1)
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//a[@class="btn btn-outline"]')))
    driver.find_element_by_xpath('//a[@class="btn btn-outline"]').click()
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//div[@class="modal-backdrop fade in"]')))
    # time.sleep(2)
    # driver.find_element_by_xpath('//button[@type="submit"]')
    WebDriverWait(driver, 15).until(
        EC.visibility_of_element_located((By.XPATH, '//input[@id="email"][@placeholder="Email"]')))

    email = driver.find_element_by_xpath('//input[@id="email"][@placeholder="Email"]')
    password = driver.find_element_by_xpath('//input[@id="password"][@placeholder="Password"]')

    email.send_keys('feiyahui000@gmail.com')
    password.send_keys('Freddy666')
    driver.find_element_by_xpath('//button[@type="submit"]').click()
    time.sleep(2)
    start_get_all_list()
    start_get_email_list()


driver = webdriver.Chrome()
print('start update tipper ')
login()
print('end update tipper ')

# notify('ltwassi', 'Combo pick')

# print(datetime.now())
# print(datetime.now()+ timedelta(hours=1))
# print(datetime.now()+ timedelta(minutes=1))
